package com.example.flink.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@RestController
public class SysInfoController {
    @GetMapping("/api/sysinfo")
    public Map<String, Object> sysInfo() {
        Map<String, Object> info = new HashMap<>();
        RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
        MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
        ThreadMXBean thread = ManagementFactory.getThreadMXBean();
        MemoryUsage heap = memory.getHeapMemoryUsage();
        MemoryUsage nonHeap = memory.getNonHeapMemoryUsage();
        File root = new File("/");
        info.put("systemTime", new java.util.Date());
        info.put("osName", System.getProperty("os.name"));
        info.put("osVersion", System.getProperty("os.version"));
        info.put("jvmVersion", System.getProperty("java.version"));
        info.put("jvmVendor", System.getProperty("java.vendor"));
        info.put("jvmStartTime", new java.util.Date(runtime.getStartTime()));
        info.put("jvmUptime", runtime.getUptime()/1000 + " 秒");
        info.put("heapUsed", heap.getUsed()/1024/1024 + " MB");
        info.put("heapMax", heap.getMax()/1024/1024 + " MB");
        info.put("nonHeapUsed", nonHeap.getUsed()/1024/1024 + " MB");
        info.put("cpuCores", Runtime.getRuntime().availableProcessors());
        info.put("threadCount", thread.getThreadCount());
        info.put("diskFree", root.getFreeSpace()/1024/1024/1024 + " GB");
        info.put("diskTotal", root.getTotalSpace()/1024/1024/1024 + " GB");
        return info;
    }
} 